/**
 * 
 */
package vue.globalBar;

import java.awt.Dimension;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JPanel;

import vue.IIGSATPart;
import vue.session.IGBlock;
import vue.session.IGCNFUse;
import vue.session.IGExecutionBlock;
import vue.session.IGPreprocessorBlock;
import vue.session.IGSolverBlock;
import controller.ISessionManager;

/**
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGGlobalCNFBar extends JPanel implements IIGSATPart {
	private static final long					serialVersionUID	= 1L;

	public static final String					BORDER_TITLE		= "CNF Execution Bar";

	public static final int						HEIGHT				= 100;
	public static final int						WIDTH				= 1200;

	private ISessionManager						manager;

	private IGBlock								selected			= null;
	private IGCNFExecutionBar					selectedBar			= null;

	private Map<IGCNFUse, IGCNFExecutionBar>	cnfuses				= new HashMap<IGCNFUse, IGCNFExecutionBar>();

	public IGGlobalCNFBar(ISessionManager manager) {
		this.manager = manager;
	}

	@Override
	public void addCNFUse(IGCNFUse cnfuse) {
		IGCNFExecutionBar bar = new IGCNFExecutionBar(cnfuse, manager);
		cnfuses.put(cnfuse, bar);
		Dimension dim = new Dimension(WIDTH, HEIGHT);

		setBorder(null);
		setPreferredSize(dim);
		setSize(dim);

	}

	@Override
	public void addExecution(IGCNFUse cnfuse, IGExecutionBlock exec) {
		cnfuses.get(cnfuse).addExecution(cnfuse, exec);
	}

	@Override
	public void addPreprocessor(IGCNFUse cnfuse, IGPreprocessorBlock preproc, IGCNFUse res) {
		cnfuses.get(cnfuse).addPreprocessor(cnfuse, preproc, res);
	}

	@Override
	public void addSolver(IGCNFUse cnfuse, IGSolverBlock solver) {
		cnfuses.get(cnfuse).addSolver(cnfuse, solver);
	}

	@Override
	public void hasBeenModified(IGBlock block) {
		for (IGCNFExecutionBar bar : cnfuses.values())
			bar.hasBeenModified(block);
	}

	@Override
	public void hasBeenRemoved(IGBlock block) {
		for (IGCNFUse cnf : cnfuses.keySet())
			if (cnf == block)
			{// retire la cnf
				IGCNFExecutionBar bar = cnfuses.remove(cnf);

				if (cnf == selected)
				{
					selected = null;
					selectedBar = null;
					remove(bar);
					repaint();
					validate();
				}
				return;
			}

		for (IGCNFExecutionBar bar : cnfuses.values())
			bar.hasBeenRemoved(block);
	}

	@Override
	public void hasBeenSelected(IGBlock block) {
		if (selected == block || block == null)
			return;

		if (selected != null)
			// remove(selectedBar);
			selected = null;
		// selectedBar = null;

		// change la bar selectionner
		for (IGCNFUse cnf : cnfuses.keySet())
			if (cnf == block)
			{
				if (selectedBar != null)
					remove(selectedBar);
				selectedBar = cnfuses.get(cnf);
				selected = cnf;
				add(selectedBar);
			}

		// maj
		for (IGCNFExecutionBar bar : cnfuses.values())
			bar.hasBeenSelected(block);

		repaint();
		validate();
	}


}
